package old; 

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import twitter4j.*;
  
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.LinkedList;
import org.apache.lucene.index.TermFreqVector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import clustering.Kmeans2;
import twitterSearch.SearchTwitter;
import twitterSearch.TwitterLuceneIndex;
import twitterSearch.TwitterLuceneSearch;
  
  public class SearchTweetsFrame extends JPanel {
      Twitter twitter;
      SearchTwitter searchEngine;
      JButton searchTweetsButton;
      JTextField searchTweetsTextField;
      private int componentsWidth = 180;
      private int panelHeight = 100;
      private String lang = "en";
      private Directory directory;
      private TwitterLuceneIndex luceneIndex;
      private TwitterLuceneSearch luceneSearch;
      private TermFreqVector[] vectors;
      private int numVectors;
      private Kmeans2 kMeans;
      private LinkedList<String> topTerms;
      
             
      public SearchTweetsFrame(final Twitter twitter) throws IOException {
          super(new FlowLayout());
          this.twitter = twitter;
          this.searchEngine = new SearchTwitter(twitter);
          this.setSize(componentsWidth, panelHeight);
          this.setPreferredSize(new Dimension(componentsWidth, panelHeight));
          this.setMaximumSize(new Dimension(componentsWidth, panelHeight));
          this.directory = getFDSDirectory();
          this.luceneIndex = new TwitterLuceneIndex(directory);
  
          searchTweetsButton = new JButton("Search Dinamically");
          searchTweetsButton.setSize(new Dimension(componentsWidth, 20));
          searchTweetsButton.setPreferredSize(new Dimension(componentsWidth, 20));
          searchTweetsTextField = new JTextField();
          searchTweetsTextField.setSize(componentsWidth, 20);
          searchTweetsTextField.setPreferredSize(new Dimension(componentsWidth, 20));
          searchTweetsTextField.setMinimumSize(new Dimension(componentsWidth, 20));
          searchTweetsButton.addActionListener(new ActionListener() {
              
            @Override
              public void actionPerformed(ActionEvent e) {
                  QueryResult q = null;

                  try {                      
                      java.util.LinkedList<Tweet> tweetsMore = searchEngine.getListSearchResults(
                              searchTweetsTextField.getText(), lang);                      
                      showResultsList(tweetsMore);                      
                      luceneIndex.addTweetsNoBlanks(tweetsMore);
                      luceneIndex.analyzeTokens(tweetsMore);                      
                      luceneSearch = new TwitterLuceneSearch (directory);
                      vectors = luceneSearch.makeVectors();
                      luceneSearch.printVectors();
                      numVectors = luceneSearch.getNumVectors();                      
                      kMeans = new Kmeans2(vectors, numVectors, luceneIndex.analyzeString(searchTweetsTextField.getText()));
                      //kMeans.runKmeans(Kmeans2.DISTANCIA_ALTERNATIVA);
                      
                  } catch (IOException ex) {
                    Logger.getLogger(SearchTweetsFrame.class.getName()).log(Level.SEVERE, null, ex);
                  } catch (TwitterException e1) {
                      JOptionPane.showMessageDialog(null, "Error occurred during search, try again.");
                  }
              }
          });
          this.add(searchTweetsButton);
          this.add(searchTweetsTextField);
      }
      
      protected void showResultsList(LinkedList<Tweet> tweetList){
          JFrame resultDialog = new JFrame();
          String[] tweetArr = new String[tweetList.size()];

          for (int i = 0; i < tweetList.size(); i++)
                tweetArr[i] = i + ") " + tweetList.get(i).getFromUser() + ":  "
                        + tweetList.get(i).getText();
          
          JList list = new JList(tweetArr);
          JScrollPane scrollPane = new JScrollPane(list,
                  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, // vertical bar
                  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          resultDialog.add(scrollPane);

          resultDialog.setSize(new Dimension(1100, 320));
          resultDialog.setPreferredSize(new Dimension(1100, 320));
          resultDialog.setLocationRelativeTo(null);
          resultDialog.setVisible(true);
      }
      
      protected Directory getFDSDirectory() throws IOException{
          String indexDir = "C:\\index-dir";
          return FSDirectory.getDirectory(indexDir, true);
      }
  }